package com.deercrowing.takeaway.controller.admin;


import com.deercrowing.takeaway.common.model.Result;
import com.deercrowing.takeaway.entity.OrdersEntity;
import com.deercrowing.takeaway.vo.OrdersPageVO;
import com.deercrowing.takeaway.vo.OrdersVO;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import com.deercrowing.takeaway.service.OrdersService;
import org.springframework.web.bind.annotation.*;


/**
 * 订单表(Orders)表控制层
 *
 * @author guoyongxiang
 * @since 2024-07-11 16:04:58
 */
@Slf4j
@Validated
@RestController
@RequestMapping("admin/orders")
public class OrdersController {
    /**
     * 服务对象
     */
    @Autowired
    private OrdersService ordersService;

    @GetMapping("/list")
    public ResponseEntity selectAll(OrdersPageVO ordersPageVO){
        if (ObjectUtils.isEmpty(ordersPageVO)){
            throw new NullPointerException();
        }

        return ResponseEntity.ok(Result.OK(this.ordersService.searchByCondition(ordersPageVO)));
    }

    @GetMapping("/status")
    public ResponseEntity selectByStatus(){
        return ResponseEntity.ok(Result.OK(this.ordersService.searchByStatus()));
    }

    @PutMapping("/rejection")
    public ResponseEntity rejection(@RequestBody OrdersVO ordersVO){
        if (ObjectUtils.isEmpty(ordersVO)){
            throw new NullPointerException();
        }

        this.ordersService.ordersByRejection(ordersVO);
        return ResponseEntity.ok(Result.OK());
    }

    @PutMapping("/confirm")
    public ResponseEntity confirm(@RequestBody OrdersVO ordersVO){
        if (ObjectUtils.isEmpty(ordersVO)){
            throw new NullPointerException();
        }

        this.ordersService.ordersByConfirm(ordersVO);
        return ResponseEntity.ok(Result.OK());
    }

    @PutMapping("/cancel")
    public ResponseEntity cancel(@RequestBody OrdersVO ordersVO) throws Exception {
        if (ObjectUtils.isEmpty(ordersVO)){
            throw new NullPointerException();
        }

        this.ordersService.ordersByCancel(ordersVO);
        return ResponseEntity.ok(Result.OK());
    }

    @PutMapping("/complete/{id}")
    public ResponseEntity complete(@PathVariable Long id,@RequestParam Short status){
        OrdersEntity ordersEntity = ordersService.getById(id);
        if (ObjectUtils.isEmpty(ordersEntity)){
            throw new NullPointerException();
        }

        this.ordersService.ordersByComplete(id,status);
        return ResponseEntity.ok(Result.OK());
    }

    @PutMapping("/delivery/{id}")
    public ResponseEntity delivery(@PathVariable Long id,@RequestParam Short status){
        OrdersEntity ordersEntity = ordersService.getById(id);
        if (ObjectUtils.isEmpty(ordersEntity)){
            throw new NullPointerException();
        }

        this.ordersService.ordersByDelivery(id,status);
        return ResponseEntity.ok(Result.OK());
    }

    @GetMapping("/details/{id}")
    public ResponseEntity details(@PathVariable @NotNull(message = "主键不能为空") Long id){
        OrdersEntity ordersEntity = ordersService.getById(id);
        if (ObjectUtils.isEmpty(ordersEntity)){
            throw new NullPointerException();
        }

        return ResponseEntity.ok(Result.OK(this.ordersService.searchById(id)));
    }
}
